package com.tiza.xiaoyoubao.ui.fragment;

import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.widget.TextView;

import com.orhanobut.hawk.Hawk;
import com.tianze.library.adapter.recycler.RecyclerBaseAdapter;
import com.tianze.library.view.EmptyLayout;
import com.tiza.xiaoyoubao.R;
import com.tiza.xiaoyoubao.app.Constants;
import com.tiza.xiaoyoubao.entity.PromoteUserInfo;
import com.tiza.xiaoyoubao.entity.UserInfo;
import com.tiza.xiaoyoubao.net.ApiHttpClient;
import com.tiza.xiaoyoubao.net.JsonCallback;
import com.tiza.xiaoyoubao.net.ResponseInfo;

import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import okhttp3.Call;

/**
 * Created by mapple on 17/3/20.
 * 推广的用户
 */

public class PromoteUserFragment extends PromoteUserRefreshFragment {

    UserInfo userInfo = Hawk.get(Constants.KEY_USER_INFO, (UserInfo) null);
    private List<PromoteUserInfo> infoList;

    @Override
    protected RecyclerBaseAdapter getListAdapter() {
        return new PromoteUserAdapter();
    }

    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        pageSize = Integer.MAX_VALUE;
    }

    @Override
    protected void onItemClick(View view, int position) {
        super.onItemClick(view, position);
        PromoteUserInfo info = (PromoteUserInfo) mAdapter.getData().get(position);
        Bundle args = new Bundle();
    }

    public static class PromoteUserAdapter extends RecyclerBaseAdapter {

        @Override
        protected View onCreateItemView(ViewGroup parent, int viewType) {
            return getLayoutInflater(parent.getContext()).inflate(R.layout.item_promote_user,
                    parent, false
            );
        }

        @Override
        protected ViewHolder onCreateItemViewHolder(View view, int viewType) {
            return new ViewHolder(viewType, view);
        }

        @Override
        public void onBindItemViewHolder(RecyclerBaseAdapter.ViewHolder holder, int position) {
            super.onBindItemViewHolder(holder, position);
            final ViewHolder vh = (ViewHolder) holder;
            PromoteUserInfo info = (PromoteUserInfo) _data.get(position);
            vh.tv_userId.setText(info.getUserId());
            vh.tv_registeredTime.setText(info.getRegisteredTime());
            vh.tv_state.setText(info.getState());
        }

        static class ViewHolder extends RecyclerBaseAdapter.ViewHolder {
            @BindView(R.id.tv_userId)
            TextView tv_userId;
            @BindView(R.id.tv_registeredTime)
            TextView tv_registeredTime;
            @BindView(R.id.tv_state)
            TextView tv_state;
            ViewHolder(int viewType, View view) {
                super(viewType, view);
                ButterKnife.bind(this, view);
            }
        }
    }

    @Override
    protected void requestData(boolean refresh) {
        super.requestData(refresh);
        ApiHttpClient.getBuyerInfoFinish(this, userInfo.getWebzzCmsBuyerInfo().getUserid(), "2", new JsonCallback<PromoteUserInfo>(PromoteUserInfo.class) {

            @Override
            public void onError(Call call, Exception e) {
                onRequestError();
            }

            @Override
            public void onResponse(ResponseInfo<PromoteUserInfo> response) {
                int code = response.getCode();
                if (code == 1) {
                    mErrorLayout.setErrorType(EmptyLayout.NETWORK_ERROR);
                    return;
                }
                infoList = response.getData();
                onRequestResponse(infoList);
            }

            @Override
            public void onAfter() {
                super.onAfter();
                onRequestFinish();
            }
        });
    }

}
